import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.awt.Toolkit;

/**
 * Creates the first setup screen for the mule game. Gives the player the
 * options to pick the game level (beginner, intermediate, or expert),
 * number of players (up to 6) and choose the type of map environment
 * ("Standard", "Desert", "Cave", "Space", "Rain-forest" or"Bikini Bottom").
 * Once this is all inputed, the start button is pressed to set the user's
 * values.
 * 
 * @author GT Five-O
 * @version v1.0b
 * 
 */
public class gameConfigurationScreen extends JFrame implements
        java.io.Serializable {
    private PlayerConfig playerInit;
    private JPanel contentPane;
    private JComboBox difLevel;
    private JComboBox playerNum;
    private JComboBox mapType;
    private String map;

    public gameConfigurationScreen() {
        map = "board.jpg";
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 811, 692);
        setSize(GameConstants.DEFAULT_WIDTH, GameConstants.DEFAULT_HEIGHT);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, GameConstants.DEFAULT_WIDTH,
                GameConstants.DEFAULT_HEIGHT);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblSelectLevel = new JLabel("Select Level:");
        lblSelectLevel.setFont(new Font("Helvetica Neue", Font.PLAIN, 13));
        lblSelectLevel.setBounds(415, 196, 88, 16);
        panel.add(lblSelectLevel);

       difLevel = new JComboBox();
       difLevel.setFont(new Font("Helvetica Neue", Font.BOLD, 13));
       difLevel.setModel(new DefaultComboBoxModel(new String[] { "Beginner",
                "Intermediate", "Expert" }));
       difLevel.setBounds(501, 192, 233, 27);
       difLevel.addKeyListener(new enterListener());
        panel.add(difLevel);

        JLabel lblEnterNumberOf = new JLabel("Enter Number of Players:");
        lblEnterNumberOf.setFont(new Font("Helvetica Neue", Font.PLAIN, 13));
        lblEnterNumberOf.setHorizontalAlignment(SwingConstants.RIGHT);
        lblEnterNumberOf.setBounds(319, 224, 171, 16);
        panel.add(lblEnterNumberOf);

        playerNum = new JComboBox();
        playerNum.setFont(new Font("Helvetica Neue", Font.BOLD, 13));
        playerNum.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
                "3", "4" }));
        playerNum.setBounds(501, 220, 162, 27);
        playerNum.addKeyListener(new enterListener());
        panel.add(playerNum);

        JLabel lblSelectMapType = new JLabel("Select Map Type:");
        lblSelectMapType.setHorizontalAlignment(SwingConstants.RIGHT);
        lblSelectMapType.setFont(new Font("Helvetica Neue", Font.PLAIN, 13));
        lblSelectMapType.setBounds(379, 249, 111, 16);
        panel.add(lblSelectMapType);

        mapType = new JComboBox();
        mapType.setFont(new Font("Helvetica Neue", Font.BOLD, 13));
        mapType.setModel(new DefaultComboBoxModel(new String[] { "Standard",
                "Bikini Bottom" }));
        mapType.setBounds(501, 245, 200, 27);
        mapType.addKeyListener(new enterListener());
        panel.add(mapType);

        JButton btnStartGame = new JButton("Start Game");
        btnStartGame.setFont(new Font("Helvetica Neue", Font.BOLD, 13));
        btnStartGame.addKeyListener(new enterListener());
        btnStartGame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String value = (String) playerNum.getSelectedItem();
                map = (String) mapType.getSelectedItem();
                int value2 = Integer.parseInt(value);
                String level = (String) difLevel.getSelectedItem();
                playerInit = new PlayerConfig(value2, level);
                playerInit.setVisible(true);
                screenClose();
            }
        });
        btnStartGame.setBounds(501, 284, 171, 29);
        panel.add(btnStartGame);

        JLabel lblNewLabel_2 = new JLabel("");
        lblNewLabel_2.setIcon(new ImageIcon("Images/start.png"));
        lblNewLabel_2.setBounds(0, 0, GameConstants.DEFAULT_WIDTH,
                GameConstants.DEFAULT_HEIGHT);
        panel.add(lblNewLabel_2);
    }

    private class enterListener implements KeyListener {

        /**
         * creates a new player with the name entered by the user and sets the
         * player color fields.
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                String value = (String) playerNum.getSelectedItem();
                map = (String) mapType.getSelectedItem();
                System.out.println("hi:" +map);
                int value2 = Integer.parseInt(value);
                String level = (String) difLevel.getSelectedItem();
                playerInit = new PlayerConfig(value2, level);
                playerInit.setVisible(true);
                screenClose();
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {

        }

        @Override
        public void keyTyped(KeyEvent arg0) {
        }

    }

    /**
     * closes the game
     */
    public void screenClose() {
        this.dispose();
    }
    
    public String getMapSelected() {
        return map;
    }

    /**
     * getter for player data
     * 
     * @return
     */
    public ArrayList<String[]> getPlayerData() {
        return playerInit.getData();
    }

    /**
     * checking if player has finished entering data in the setup screens.
     * 
     * @return
     */
    public boolean isNotFinished() {
        return (!playerInit.isDone());
    }
}
